using System;
using System.Collections.Generic;
using Data;
using TMPro;
using UnityEngine;

public class CollectionPopupHandler : MonoBehaviour, IPopupWnd, IHandlerBase
{
	public void DoDataExchange()
	{
	}

	public void OnEnterPopup(object param)
	{
		this.themeName = (string)param;
		this.title.text = StringExt.UppercaseFirst(this.themeName);
		foreach (PictureWidgetHandler pictureWidgetHandler in this.widgets)
		{
			UnityEngine.Object.Destroy(pictureWidgetHandler.gameObject);
		}
		this.widgets.Clear();
		this.top.transform.localPosition = new Vector3(this.top.transform.localPosition.x, BlockBannerAnchor.GetTopPositionY(), this.top.transform.localPosition.z);
		float bannerPositionY = BlockBannerAnchor.GetBannerPositionY();
		this.bottom.transform.localPosition = new Vector3(this.bottom.transform.localPosition.x, bannerPositionY, this.bottom.transform.localPosition.z);
		this.CreatePictureList();
		int num = this.scrollRecord.FindIndex((ScrollRecord item) => item.theme == this.themeName);
		if (num >= 0)
		{
			this.pageArea.Value = this.scrollRecord[num].yValue;
		}
		else
		{
			this.pageArea.Value = 0f;
		}
	}

	public void OnLeavePopup()
	{
		int num = this.scrollRecord.FindIndex((ScrollRecord item) => item.theme == this.themeName);
		if (num >= 0)
		{
			this.scrollRecord[num].yValue = this.pageArea.Value;
		}
		else
		{
			this.scrollRecord.Add(new ScrollRecord(this.themeName, this.pageArea.Value));
		}
		foreach (PictureWidgetHandler pictureWidgetHandler in this.widgets)
		{
			UnityEngine.Object.Destroy(pictureWidgetHandler.gameObject);
		}
		this.widgets.Clear();
	}

	private bool CreatePictureList()
	{
		Vector3 zero = Vector3.zero;
		Vector3 vector = Scene.CurrentPopup().transform.InverseTransformPoint(this.topDown.transform.position);
		Vector3 vector2 = Scene.CurrentPopup().transform.InverseTransformPoint(this.bottomUp.transform.position);
		zero.y = vector.y;
		zero.z = -1f;
		zero.y -= 4f;
		List<PictureWidgetHandler> list = new List<PictureWidgetHandler>();
		this.totalCount = 0;
		this.clearCount = 0;
		for (int i = 0; i < DataContainer<Data.Root>.Data.packages.Length; i++)
		{
			for (int j = 0; j < DataContainer<Data.Root>.Data.packages[i].levels.Length; j++)
			{
				if (!string.IsNullOrEmpty(DataContainer<Data.Root>.Data.packages[i].levels[j].pictureTheme) && this.themeName.Equals(DataContainer<Data.Root>.Data.packages[i].levels[j].pictureTheme))
				{
					PictureWidgetHandler pictureWidgetHandler = Widget.Create<PictureWidgetHandler>();
					pictureWidgetHandler.transform.parent = this.pageContents.transform;
					zero.x = ((this.totalCount % 2 != 0) ? 3.1f : -3.1f);
					pictureWidgetHandler.transform.localPosition = zero;
					if (this.totalCount % 2 != 0)
					{
						zero.y -= this.widgetInterval;
					}
					pictureWidgetHandler.transform.localScale = Vector3.one;
					pictureWidgetHandler.gameObject.name = string.Format("{0:D3}.{1}", this.totalCount, pictureWidgetHandler.gameObject.name);
					pictureWidgetHandler.SetData(base.gameObject, DataContainer<Data.Root>.Data.packages[i], DataContainer<Data.Root>.Data.packages[i].levels[j]);
					this.widgets.Add(pictureWidgetHandler);
					list.Add(pictureWidgetHandler);
					if (DataContainer<Data.Root>.Data.packages[i].levels[j].record.isClear)
					{
						this.clearCount++;
					}
					this.totalCount++;
				}
			}
		}
		this.collectionCount.text = string.Format("{0}/{1}", this.clearCount, this.totalCount);
		GameObject gameObject = this.pageContents.gameObject;
		Bounds bounds = new Bounds(gameObject.transform.position, Vector3.zero);
		for (int k = 0; k < list.Count; k++)
		{
			bounds.Encapsulate(list[k].GetComponent<Renderer>().bounds);
		}
		this.pageArea.ContentLength = bounds.size.y + 2f;
		this.pageArea.VisibleAreaLength = Mathf.Abs(vector.y) + Mathf.Abs(vector2.y);
		return true;
	}

	private void OnClick_Form_back()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		BlockPopupTransition.PopupAnimation(delegate
		{
			Scene.ClosePopup("CollectionPopup", null);
		});
	}

	public void OnClickPicture(PictureWidgetHandler _picture)
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		if (_picture.picturelevel.record.isNewCollection)
		{
			_picture.picturelevel.record.isNewCollection = false;
			_picture.picturelevel.SaveContext();
		}
		Scene.ShowPopup("PicturePopup", _picture.picturelevel, delegate(object close)
		{
			_picture.newIcon.SetActive(false);
		});
	}

	private void OnEscape()
	{
		BlockPopupTransition.PopupAnimation(delegate
		{
			Scene.ClosePopup("CollectionPopup", null);
		});
	}

	private void ScrollYCallBack()
	{
		if (this.pageArea.Value >= 0.025f)
		{
			if (this.pageArea.Value > 0.975f)
			{
			}
		}
		if (this.pageArea.ContentLength < this.pageArea.VisibleAreaLength)
		{
		}
	}

	[SerializeField]
	private tk2dCamera cam;

	[SerializeField]
	private GameObject curtain;

	public GameObject pageContents;

	public tk2dUIScrollableArea pageArea;

	public GameObject top;

	public GameObject topDown;

	public GameObject bottom;

	public GameObject bottomUp;

	public TextMeshPro title;

	public TextMeshPro collectionCount;

	private int currentPage;

	private List<PictureWidgetHandler> widgets = new List<PictureWidgetHandler>();

	private float originalwidgetInterval = 4f;

	private float widgetInterval = 6.5f;

	private string themeName = string.Empty;

	private int totalCount;

	private int clearCount;

	private List<ScrollRecord> scrollRecord = new List<ScrollRecord>();
}
